package MemberService

import (
	"gdshop-back-end-go/client-api/app/request/MemberReq"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/response"
	"gdshop-back-end-go/common/tools"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func MyRefundList(r *ghttp.Request, parames *MemberReq.MyRefundList) *response.JsonResponse {
	ctx := r.GetCtx()
	obj := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "order_sub").As("os").LeftJoin("order o", "o.id = os.order_id").
		Where("os.refund_status > 0 AND o.member_id = ?", parames.MemberId)
	count, err := obj.Fields("1").Count()
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	all, err := obj.Fields("os.*,ore.refund_type,ore.reason_type,ore.apply_money").
		LeftJoin("order_refund ore", "ore.order_sub_id = os.id").Order("ore.id DESC").
		Page(parames.Page, parames.PageSize).All()
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}

	return response.SuccessByRequestMessageData(r, "成功", g.Map{
		"page_info":      tools.GetPageInfo(count, parames.Page, parames.PageSize),
		"photo_base_url": g.Cfg().GetString("site.PhotoUrlProfix"),
		"status_text": g.Map{
			"0":  "正常",
			"1":  "申请中",
			"2":  "驳回",
			"3":  "待退款",
			"99": "完成",
		},
		"refund_type_text": g.Map{
			"1": "仅退款",
			"2": "退货退款",
			"3": "换货",
		},
		"list": all,
	})
}
